package controller;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sun.org.apache.xml.internal.utils.SuballocatedByteVector;

/**
 * @author chengzb
 *@date 2017年1月10日上午11:30:37
 * @Description: TODO
 */
@Controller
@RequestMapping("/socket")
public class SocketController {
@RequestMapping("/index")
public ModelAndView index(){
	ModelAndView view=new ModelAndView("/index");
	return view;
}
/**
 * 清除流量计
 * 
 * @return
 */
@ResponseBody
@RequestMapping("/clear")
public Map<String,Double> clear(){
	Map<String,Double> map=new HashMap<String, Double>();
	double a=getFlow(1);
	double b=getFlow(2);
	double c=getFlow(3);
	map.put("a",a);
	map.put("b",b);
	map.put("c",c);
     return map;
}
/**
 * 开阀
 */
@RequestMapping("/start")
@ResponseBody
public Map<String,String> start(int id){
	Map<String,String> map=new HashMap<String, String>();
	  byte[] b = new byte[8];
	  if(id==1){
		  b[0] = (byte) 0x01;
		     b[1] = (byte) 0x06;
		     b[2] = (byte) 0x0F;
		     b[3] = (byte) 0xA1;
		     b[4] = (byte) 0x00;
		     b[5] = (byte) 0x5A;
		     b[6] = (byte) 0x5B;
		     b[7] = (byte) 0x07;
	  }else if(id==2){
		  b[0] = (byte) 0x01;
		     b[1] = (byte) 0x06;
		     b[2] = (byte) 0x0F;
		     b[3] = (byte) 0xA2;
		     b[4] = (byte) 0x00;
		     b[5] = (byte) 0x5A;
		     b[6] = (byte) 0xAB;
		     b[7] = (byte) 0x07;
	  }else if(id==3){
		  b[0] = (byte) 0x01;
		     b[1] = (byte) 0x06;
		     b[2] = (byte) 0x0F;
		     b[3] = (byte) 0xA3;
		     b[4] = (byte) 0x00;
		     b[5] = (byte) 0x5A;
		     b[6] = (byte) 0xFA;
		     b[7] = (byte) 0xC7;
	  }
     byte buff[]=tcp(b);
     if(buff==null){
    	 map.put("code","0");
    	 return map;
     }
     if(bytes2hex(b).equals(bytes2hex(buff))){
    	 //成功
    	 map.put("code","1");
     }else{
    	 //失败
    	 map.put("code","0");
     }
     return map;
}
/**
 * 关阀
 */
@RequestMapping("/stop")
@ResponseBody
public Map<String,String> stop(int id){
	Map<String,String> map=autoStop(id);
     return map;
}
public Map<String,String> autoStop(int id){
	Map<String,String> map=new HashMap<String, String>();
	  byte[] b = new byte[8];
	  if(id==1){
		  	b[0] = (byte) 0x01;
		     b[1] = (byte) 0x06;
		     b[2] = (byte) 0x0F;
		     b[3] = (byte) 0xA1;
		     b[4] = (byte) 0x00;
		     b[5] = (byte) 0x50;
		     b[6] = (byte) 0xDB;
		     b[7] = (byte) 0x00;
	  }else if(id==2){
		  b[0] = (byte) 0x01;
		     b[1] = (byte) 0x06;
		     b[2] = (byte) 0x0F;
		     b[3] = (byte) 0xA2;
		     b[4] = (byte) 0x00;
		     b[5] = (byte) 0x50;
		     b[6] = (byte) 0x2B;
		     b[7] = (byte) 0x00;
	  }else if(id==3){
		  b[0] = (byte) 0x01;
		     b[1] = (byte) 0x06;
		     b[2] = (byte) 0x0F;
		     b[3] = (byte) 0xA3;
		     b[4] = (byte) 0x00;
		     b[5] = (byte) 0x50;
		     b[6] = (byte) 0x7A;
		     b[7] = (byte) 0xC0;
	  }
   byte buff[]=tcp(b);
   if(buff==null){
  	 map.put("code","0");
  	 return map;
   }
   if(bytes2hex(b).equals(bytes2hex(buff))){
  	 //成功
  	 map.put("code","1");
   }else{
  	 //失败
  	 map.put("code","0");
   }
   return map;
}
/**
 * 
 * @param data
 * @return 预设流量
 */
@RequestMapping("/order")
@ResponseBody
public Map<String,String> order(String data){
	//预设流量
	Map<String,String> map=new HashMap<String, String>();
		int da[]=Data.getData();
		String[] str=data.split(",");
		for(int i=0;i<da.length;i++){
			da[i]=Integer.valueOf(str[i]);
		}
		map.put("code","1");
		return map;
}
/**
 * 开阀 所有     data 流量数据
 */ 
@RequestMapping("/startAll")
@ResponseBody
public Map<String,String> startAll(){
	Map<String,String> map=new HashMap<String, String>();
	int num=0;
	  byte[] b = new byte[8];
	  //1 号阀门
	 b[0] = (byte) 0x01;
     b[1] = (byte) 0x06;
     b[2] = (byte) 0x0F;
     b[3] = (byte) 0xA1;
     b[4] = (byte) 0x00;
     b[5] = (byte) 0x5A;
     b[6] = (byte) 0x5B;
     b[7] = (byte) 0x07;
     byte buff[]=tcp(b);
     if(buff==null){
    	 map.put("code","0");
    	 return map;
     }
     if(bytes2hex(b).equals(bytes2hex(buff))){
    	 //成功
    	 num=num+1;
     }
     //2 号阀门
     b[0] = (byte) 0x01;
     b[1] = (byte) 0x06;
     b[2] = (byte) 0x0F;
     b[3] = (byte) 0xA2;
     b[4] = (byte) 0x00;
     b[5] = (byte) 0x5A;
     b[6] = (byte) 0xAB;
     b[7] = (byte) 0x07;
      buff=tcp(b);
      if(buff==null){
     	 map.put("code","0");
     	 return map;
      }
     if(bytes2hex(b).equals(bytes2hex(buff))){
    	 //成功
    	 num=num+1;
     }
     //3 号阀门
     b[0] = (byte) 0x01;
     b[1] = (byte) 0x06;
     b[2] = (byte) 0x0F;
     b[3] = (byte) 0xA3;
     b[4] = (byte) 0x00;
     b[5] = (byte) 0x5A;
     b[6] = (byte) 0xFA;
     b[7] = (byte) 0xC7;
      buff=tcp(b);
      if(buff==null){
     	 map.put("code","0");
     	 return map;
      }
     if(bytes2hex(b).equals(bytes2hex(buff))){
    	 //成功
    	 num=num+1;
     }
     if(num==3){
    	 map.put("code","1");
     }else{
    	 map.put("code","0");
     }
     return map;
}
/**
 * 关阀 所有
 */
@RequestMapping("/stopAll")
@ResponseBody
public Map<String,String> stopAll(){
	Map<String,String> map=new HashMap<String, String>();
	int num=0;
	  byte[] b = new byte[8];
	  //1 号阀门
	 b[0] = (byte) 0x01;
     b[1] = (byte) 0x06;
     b[2] = (byte) 0x0F;
     b[3] = (byte) 0xA1;
     b[4] = (byte) 0x00;
     b[5] = (byte) 0x50;
     b[6] = (byte) 0xDB;
     b[7] = (byte) 0x00;
     byte buff[]=tcp(b);
     if(buff==null){
    	 map.put("code","0");
    	 return map;
     }
     if(bytes2hex(b).equals(bytes2hex(buff))){
    	 //成功
    	 num=num+1;
     }
     //2 号阀门
     b[0] = (byte) 0x01;
     b[1] = (byte) 0x06;
     b[2] = (byte) 0x0F;
     b[3] = (byte) 0xA2;
     b[4] = (byte) 0x00;
     b[5] = (byte) 0x50;
     b[6] = (byte) 0x2B;
     b[7] = (byte) 0x00;
      buff=tcp(b);
      if(buff==null){
     	 map.put("code","0");
     	 return map;
      }
     if(bytes2hex(b).equals(bytes2hex(buff))){
    	 //成功
    	 num=num+1;
     }
     //3 号阀门
     b[0] = (byte) 0x01;
     b[1] = (byte) 0x06;
     b[2] = (byte) 0x0F;
     b[3] = (byte) 0xA3;
     b[4] = (byte) 0x00;
     b[5] = (byte) 0x50;
     b[6] = (byte) 0x7A;
     b[7] = (byte) 0xC0;
      buff=tcp(b);
      if(buff==null){
     	 map.put("code","0");
     	 return map;
      }
     if(bytes2hex(b).equals(bytes2hex(buff))){
    	 //成功
    	 num=num+1;
     }
     if(num==3){
    	 map.put("code","1");
     }else{
    	 map.put("code","0");
     }
     return map;
}
/**
 * 
 * @param id
 * @return 获取当前设备的 流量计 auto 1 自动
 */
@RequestMapping("/getCurrentFlow")
@ResponseBody
public   Map<String,String> getCurrentFlow(int id,int isAuto,double preFlow){
	Map<String,String> map=new HashMap<String, String>();
	double returnStr=getFlow(id);
	if(returnStr==-1){
		map.put("code","-1");//没有数据
		return map;
	}
	double da=sub(returnStr,preFlow);
	if(isAuto==1){
		//自动模式
			int data=Data.getData()[id-1];
			if(data-da<2.5){
				//自动关阀
				Map<String,String> m=autoStop(id);
				if("1".equals(m.get("code"))){
					map.put("code","2");//关阀
					return map;
				}
			}
	}
	DecimalFormat df= new DecimalFormat("######0.000");
	String result=df.format(da);
	map.put("code","1");//没有数据
	map.put("result",result);
	return map;
}
public synchronized double sub(double v1,double v2){
    BigDecimal b1 = new BigDecimal(Double.toString(v1));
    BigDecimal b2 = new BigDecimal(Double.toString(v2));
    return b1.subtract(b2).doubleValue();
} 
/**
 * 获取总流量
 * @param buff
 * @return
 *//*
@RequestMapping("/getTotalFlow")
@ResponseBody
public  Map<String,String> getTotalFlow(){
	Map<String,String> map=new HashMap<String, String>();
	double returnStr=0;
	for(int i=0;i<3;i++){
		returnStr=returnStr+getFlow(i+1);
	}
	DecimalFormat df= new DecimalFormat("######0.000");
	String result=df.format(returnStr);
	map.put("result",result);
	return map;
}*/
public  double getFlow(int id){
	byte[] zhengshu = new byte[8];
	byte[] xiaoshu = new byte[8];
	if(id==1){
		//1号阀门
		zhengshu[0] = 0x02;
		zhengshu[1] = (byte) 0x04;
		zhengshu[2] = (byte) 0x10;
		zhengshu[3] = (byte) 0x18;
		zhengshu[4] = (byte) 0x00;
		zhengshu[5] = (byte) 0x02;
		zhengshu[6] = (byte) 0xF5;
		zhengshu[7] = (byte) 0x3F;
		
		xiaoshu[0] = 0x02;
		xiaoshu[1] = (byte) 0x04;
		xiaoshu[2] = (byte) 0x10;
		xiaoshu[3] = (byte) 0x1A;
		xiaoshu[4] = (byte) 0x00;
		xiaoshu[5] = (byte) 0x02;
		xiaoshu[6] = (byte) 0x54;
		xiaoshu[7] = (byte) 0xFF;
	}else if(id==2){
		//2号阀门
				zhengshu[0] = 0x03;
				zhengshu[1] = (byte) 0x04;
				zhengshu[2] = (byte) 0x10;
				zhengshu[3] = (byte) 0x18;
				zhengshu[4] = (byte) 0x00;
				zhengshu[5] = (byte) 0x02;
				zhengshu[6] = (byte) 0xF4;
				zhengshu[7] = (byte) 0xEE;
				
				xiaoshu[0] = 0x03;
				xiaoshu[1] = (byte) 0x04;
				xiaoshu[2] = (byte) 0x10;
				xiaoshu[3] = (byte) 0x1A;
				xiaoshu[4] = (byte) 0x00;
				xiaoshu[5] = (byte) 0x02;
				xiaoshu[6] = (byte) 0x55;
				xiaoshu[7] = (byte) 0x2E;
	}else if(id==3){
		//3号阀门
		zhengshu[0] = 0x04;
		zhengshu[1] = (byte) 0x04;
		zhengshu[2] = (byte) 0x10;
		zhengshu[3] = (byte) 0x18;
		zhengshu[4] = (byte) 0x00;
		zhengshu[5] = (byte) 0x02;
		zhengshu[6] = (byte) 0xF5;
		zhengshu[7] = (byte) 0x59;
		
		xiaoshu[0] = 0x04;
		xiaoshu[1] = (byte) 0x04;
		xiaoshu[2] = (byte) 0x10;
		xiaoshu[3] = (byte) 0x1A;
		xiaoshu[4] = (byte) 0x00;
		xiaoshu[5] = (byte) 0x02;
		xiaoshu[6] = (byte) 0x54;
		xiaoshu[7] = (byte) 0x99;
	}
	//获取整数部分
    byte buff[]=tcp(zhengshu);
    if(buff==null){
    	return -1;
    }
    String hex=bytes2hex(buff).substring(6,14);
    Integer zheng=null;
    try{
    	zheng= Integer.valueOf(hex,16);
    }catch (Exception e) {
   	 //解析异常
   	 System.out.println("==========解析异常");
   	 return -1;
	}
    
    //获取小数部分
    byte bu[]=tcp(xiaoshu);
    if(bu==null){
    	return -1;
    }
     hex=bytes2hex(bu).substring(6,14);
     Integer vaule=null;
     try{
    	   vaule= Integer.valueOf(hex,16);
     }catch (Exception e) {
    	 //解析异常
    	 System.out.println("==========解析异常");
    	 return -1;
	}
    Float  value=Float.intBitsToFloat(vaule);
	BigDecimal bd = new BigDecimal(value); 
	return zheng+bd.doubleValue();
}
public  synchronized byte[] tcp(byte[] b){
	 byte bytebuf[] = new byte[8];
	 InputStream in=null;
	 OutputStream socketWriter=null;
	 Socket socket=null;
	 //122.228.19.59  15093
	 //192.168.2.254 89
	 try {
		socket = new Socket("122.228.19.59",15093);
		 socket.setSoTimeout(3*1000);
	     socketWriter=socket.getOutputStream();
	     socketWriter.write(b);
	     socketWriter.flush();
	     in= socket.getInputStream();
	         in.read(bytebuf);
     } catch (Exception e) {
         System.out.println("can not listen to:" + e);// 出错，打印出错信息
         return null;
     }finally{
    	 try {
    		 socketWriter.close();
			in.close();
			socket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
     }
	 return bytebuf;
}
	 public  String bytes2hex(byte[] bytes)  
	    {  
	        final String HEX = "0123456789abcdef";  
	        StringBuilder sb = new StringBuilder(bytes.length * 2);  
	        for (byte b : bytes)  
	        {  
	            // 取出这个字节的高4位，然后与0x0f与运算，得到一个0-15之间的数据，通过HEX.charAt(0-15)即为16进制数  
	            sb.append(HEX.charAt((b >> 4) & 0x0f));  
	            // 取出这个字节的低位，与0x0f与运算，得到一个0-15之间的数据，通过HEX.charAt(0-15)即为16进制数  
	            sb.append(HEX.charAt(b & 0x0f));  
	        }  
	        return sb.toString();  
	    }
	 /*public   double getFlow2(int id){
			byte[] zhengshu = new byte[8];
			byte[] xiaoshu = new byte[8];
			if(id==1){
				//1号阀门
				zhengshu[0] = 0x02;
				zhengshu[1] = (byte) 0x04;
				zhengshu[2] = (byte) 0x10;
				zhengshu[3] = (byte) 0x18;
				zhengshu[4] = (byte) 0x00;
				zhengshu[5] = (byte) 0x02;
				zhengshu[6] = (byte) 0xF5;
				zhengshu[7] = (byte) 0x3F;
				
				xiaoshu[0] = 0x02;
				xiaoshu[1] = (byte) 0x04;
				xiaoshu[2] = (byte) 0x10;
				xiaoshu[3] = (byte) 0x1A;
				xiaoshu[4] = (byte) 0x00;
				xiaoshu[5] = (byte) 0x02;
				xiaoshu[6] = (byte) 0x54;
				xiaoshu[7] = (byte) 0xFF;
			}else if(id==2){
				//2号阀门
						zhengshu[0] = 0x03;
						zhengshu[1] = (byte) 0x04;
						zhengshu[2] = (byte) 0x10;
						zhengshu[3] = (byte) 0x18;
						zhengshu[4] = (byte) 0x00;
						zhengshu[5] = (byte) 0x02;
						zhengshu[6] = (byte) 0xF4;
						zhengshu[7] = (byte) 0xEE;
						
						xiaoshu[0] = 0x03;
						xiaoshu[1] = (byte) 0x04;
						xiaoshu[2] = (byte) 0x10;
						xiaoshu[3] = (byte) 0x1A;
						xiaoshu[4] = (byte) 0x00;
						xiaoshu[5] = (byte) 0x02;
						xiaoshu[6] = (byte) 0x55;
						xiaoshu[7] = (byte) 0x2E;
			}else if(id==3){
				//3号阀门
				zhengshu[0] = 0x04;
				zhengshu[1] = (byte) 0x04;
				zhengshu[2] = (byte) 0x10;
				zhengshu[3] = (byte) 0x18;
				zhengshu[4] = (byte) 0x00;
				zhengshu[5] = (byte) 0x02;
				zhengshu[6] = (byte) 0xF5;
				zhengshu[7] = (byte) 0x59;
				
				xiaoshu[0] = 0x04;
				xiaoshu[1] = (byte) 0x04;
				xiaoshu[2] = (byte) 0x10;
				xiaoshu[3] = (byte) 0x1A;
				xiaoshu[4] = (byte) 0x00;
				xiaoshu[5] = (byte) 0x02;
				xiaoshu[6] = (byte) 0x54;
				xiaoshu[7] = (byte) 0x99;
			}
			//获取整数部分
		    byte buff[]=tcp(zhengshu);
		    if(buff==null){
		    	return -1;
		    }
		    String hex=bytes2hex(buff).substring(6,14);
		    Integer zheng=null;
		    try{
		    	zheng= Integer.valueOf(hex,16);
		    }catch (Exception e) {
		   	 //解析异常
		   	 System.out.println("==========解析异常");
		   	 return -1;
			}
		    
		    //获取小数部分
		    byte bu[]=tcp(xiaoshu);
		    if(bu==null){
		    	return -1;
		    }
		     hex=bytes2hex(bu).substring(6,14);
		     Integer vaule=null;
		     try{
		    	   vaule= Integer.valueOf(hex,16);
		     }catch (Exception e) {
		    	 //解析异常
		    	 System.out.println("==========解析异常");
		    	 return -1;
			}
		    Float  value=Float.intBitsToFloat(vaule);
			BigDecimal bd = new BigDecimal(value); 
			return zheng+bd.doubleValue();
		}
	 public static void main(String[] args) {
		 SocketController s=new SocketController();
		 for(int i=0;i<20;i++){
			 System.out.println(s.getFlow2(2));
		 }
	}*/
}
